package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.security.Key;
import java.util.List;

/**
 * spring容器启动监听器,拥有初始化redis数据
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    /**
     * spring容器启动后马上去执行
     * @param contextRefreshedEvent
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("-------vo数据初始化------start----------------");
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : list) {
            //判断redis缓存中是否已经存在明细的vo
            if (strategyStatisRedisService.isStrategyStatisVOExsit(detail.getId())){
                continue;
            }

            vo = new StrategyStatisVO();
            try {
                BeanUtils.copyProperties(vo,detail);//底层使用javabean内省机制
                vo.setDestId(detail.getDest().getId());
                vo.setStrategyId(detail.getId());
                vo.setDestName(detail.getDest().getName());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            strategyStatisRedisService.setStrategyStatisVO(vo);
        }
        System.out.println("-------------end-------------");


        System.out.println("----------zset数据初始化start---------------");

        List<StrategyStatisVO> vos = strategyStatisRedisService.getStrategyStatisVOS(RedisKeys.STRATEGY_STATIS_VO.getPrefix());
        for (StrategyStatisVO detail : vos) {
            //先获取明细vo相对应的key,作为redis缓存的value
            String value = RedisKeys.STRATEGY_STATIS_VO.join(detail.getStrategyId().toString());
            //判断zset中是否存在数据,如果存在,则不覆盖,直接跳过
            if (strategyStatisRedisService.isZsetHasValue(RedisKeys.STRATEGY_STATIS_VO.getPrefix(),value)){
                continue;
            }
            //点赞数+收藏数
            int score = detail.getThumbsupnum()+detail.getFavornum();
            strategyStatisRedisService.addCommendScores(value,score);

        }
        for (StrategyStatisVO detail : vos) {
            String value = RedisKeys.STRATEGY_STATIS_VO.join(detail.getStrategyId().toString());
            if (strategyStatisRedisService.isZsetHasValue(RedisKeys.STRATEGY_STATIS_VO.getPrefix(),value)){
                continue;
            }
            int score = detail.getThumbsupnum()+detail.getFavornum();
            strategyStatisRedisService.addHotScores(value,score);
        }
        System.out.println("-----------zset数据初始化end----------");
    }

}
